package com.xmkj.carwash.count;

import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.common.carbean.CountInfoBean;
import com.common.cons.Constans;
import com.common.mvp.BaseMvpActivity;
import com.common.mvp.BasePresenter;
import com.common.retrofit.methods.LoginMethods;
import com.common.retrofit.subscriber.CommonSubscriber;
import com.common.retrofit.subscriber.SubscriberListener;
import com.common.utils.EmptyUtils;
import com.common.utils.RegexUtils;
import com.common.utils.SPUtils;
import com.xmkj.carwash.MainActivity;
import com.xmkj.carwash.R;

import butterknife.BindView;
import butterknife.ButterKnife;
import cn.jpush.android.api.JPushInterface;

/**
 * Created by chen on 2018/3/22.
 */

public class LoginActivtiy extends BaseMvpActivity {
    @BindView(R.id.et_phone)
    EditText mEtPhone;
    @BindView(R.id.et_psw)
    EditText mEtPsw;
    @BindView(R.id.tv_register)
    TextView mTvRegister;
    @BindView(R.id.tv_forget_psw)
    TextView mTvForgetPsw;
    @BindView(R.id.tv_intrue)
    TextView mTvIntrue;
    @BindView(R.id.flag)
    TextView mFlag;
    @BindView(R.id.iv_qq_login)
    ImageView mIvQqLogin;
    @BindView(R.id.iv_weichat_login)
    ImageView mIvWeichatLogin;
    private  String jpushid = "";
    @Override
    protected BasePresenter createPresenterInstance() {
        return null;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_login;
    }

    @Override
    protected void onViewCreated() {

    }

    @Override
    protected void doLogicFunc() {
        attachClickListener(mTvForgetPsw);
        attachClickListener(mTvRegister);
        attachClickListener(mTvIntrue);
    }

    @Override
    protected void onViewClicked(View view) {
        if(view.getId()==mTvForgetPsw.getId()){
            gotoActivity(FindMyPswActivity.class);
        } else if(view.getId()==mTvRegister.getId()){
            gotoActivity(RegisterActivity.class);
        }else if(view.getId()==mTvIntrue.getId()){
            if (EmptyUtils.isEmpty(getEditTextStr(mEtPhone))) {
                showToastMsg("请输入手机号");
                return;
            } else if (!RegexUtils.isMobileExact(getEditTextStr(mEtPhone))) {
                showToastMsg("手机号不正确");
                return;
            } else  if (EmptyUtils.isEmpty(getEditTextStr(mEtPsw))) {
                showToastMsg("请输入密码");
                return;
            }
            gotoLogin();
        }
    }

    private void gotoLogin() {
        showProgressingDialog();
        CommonSubscriber<CountInfoBean> subscriber = new CommonSubscriber<>(new SubscriberListener() {
            @Override
            public void onNext(Object o) {
                CountInfoBean infoBean = (CountInfoBean) o;
                SPUtils.setShareInt(Constans.UID,infoBean.uid);
                SPUtils.setShareString(Constans.HASHID,infoBean.hashid);
                dismissProgressDialog();
                gotoActivity(MainActivity.class);
            }

            @Override
            public void onError(String e, int code) {
                dismissProgressDialog();
                showToastMsg(e);
            }
        });

        jpushid = JPushInterface.getRegistrationID(this);
        LoginMethods.getInstance().login(subscriber,getEditTextStr(mEtPhone),getEditTextStr(mEtPsw),jpushid);
        rxManager.add(subscriber);
    }

    @Override
    protected void setStatusBar() {

    }



    @Override
    public void bindKnife() {
        ButterKnife.bind(this);
    }
}
